package com.jiazaibendi.lottery.controller;

import com.jiazaibendi.lottery.common.CommonResponse;
import com.jiazaibendi.lottery.common.CommonResponseEnum;
import com.jiazaibendi.lottery.service.OperationService;
import com.jiazaibendi.lottery.service.PrizePoolService;
import com.jiazaibendi.lottery.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * description
 *
 * @author echo_kai
 * @version 1.0
 * @date 2021/08/06
 */
@CrossOrigin
@RestController("/api/lottery")
public class OperateController {

    @Autowired
    private RecordService recordService;
    
    @Autowired
    private OperationService operationService;

    @Autowired
    private PrizePoolService prizePoolService;
    
    // 查询参与游戏人数
    @GetMapping("/participantNumber")
    public CommonResponse queryParticipants(@RequestParam long schemeId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        response.setData(recordService.queryAllJoinPeopleNumber(schemeId));
        return response;
    }

    // 查询获奖人数
    @GetMapping("/winnerNumber")
    public CommonResponse queryWinner(@RequestParam long schemeId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        response.setData(recordService.queryGainPrizePeopleNumber(schemeId));
        return response;
    }

    // 查询获奖记录
    @GetMapping("/winnerRecord")
    public CommonResponse queryWinnerRecord(@RequestParam long schemeId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        response.setData(recordService.queryPrizeRecord(schemeId));
        return response;
    }

    // 查询我的奖品
    @GetMapping("/myPrize")
    public CommonResponse queryMyPrizes(@RequestParam long schemeId, @RequestParam long userId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        response.setData(recordService.queryMyPrizes(schemeId, userId));
        return response;
    }

    // 抽奖
    @GetMapping("/getTicket")
    public CommonResponse queryTicket(@RequestParam long schemeId, @RequestParam long userId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        response.setData(operationService.playGame(userId, schemeId));
        return response;
    }
    
    @PostMapping("/addCode")
    public CommonResponse addCode(@RequestParam List<String> codeList, @RequestParam long schemeId, @RequestParam long prizeId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        prizePoolService.addCodeToEnvelopePool(schemeId,prizeId,codeList);
        response.setMsg("添加成功");
        return response;
    }

    @GetMapping("/updateRecord")
    public CommonResponse updateRecord(@RequestParam long recordId, @RequestParam long telephone, @RequestParam String name) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        recordService.updateWinRecord(recordId, telephone, name);
        response.setMsg("更新成功");
        return response;
    }

    @GetMapping("/getUserId")
    public CommonResponse getUserId(@RequestParam long telephone) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        response.setData(recordService.getUserIdByTelephone(telephone));
        return response;
    }

    @GetMapping("/pyqCallback")
    public CommonResponse pyqCallback(@RequestParam long userId, @RequestParam long schemeId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        operationService.addFrequencyByFriendCircle(userId, schemeId);
        response.setMsg("回调成功");
        return response;
    }

    @GetMapping("/groupCallback")
    public CommonResponse groupCallback(@RequestParam long userId, @RequestParam long schemeId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        response.setData(operationService.addFrequencyByGroup(userId, schemeId));
        response.setMsg("回调成功");
        return response;
    }

    @GetMapping("/friendCallback")
    public CommonResponse friendCallback(@RequestParam long userId, @RequestParam long schemeId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        response.setData(operationService.addFrequencyByFriend(userId, schemeId));
        response.setMsg("回调成功");
        return response;
    }

    @GetMapping("/getRemaining")
    public CommonResponse getRemaining(@RequestParam long userId, @RequestParam long schemeId) {
        CommonResponse response = new CommonResponse();
        response.setCode(CommonResponseEnum.SUCCESS.getCode());
        response.setData(operationService.queryUserRemainingFrequency(userId, schemeId));
        return response;
    }
}
